package cn.quevo.cube.provide.define.validate.impl;

import cn.quevo.common.lang.BooleanUtils;
import cn.quevo.common.lang.NumberUtils;
import cn.quevo.common.lang.StringUtils;
import cn.quevo.cube.provide.define.entity.dto.CubeColumnDto;
import cn.quevo.cube.provide.define.validate.AbstractColumnTypeValidate;
import cn.quevo.cube.provide.define.validate.ColumnTypeValidate;
import cn.quevo.cube.provide.define.validate.tools.ColumnExtTool;
import cn.quevo.cube.provide.exception.ColumnCheckException;
import cn.quevo.cube.provide.exception.response.ColumnExceptionResponse;

/**
 * 文件上传验证
 *
 * @author July july_sky@foxmail.com
 * @version 1.0
 * @date 2018/12/14 5:41 PM
 * @Copyright 湘豫（北京）科技有限公司版权所有.All Rights Reserved.
 */
public abstract class UploadValidate extends AbstractColumnTypeValidate implements ColumnTypeValidate {

    @Override
    protected void checked(CubeColumnDto cubeColumnDto) {
        String fileSize = super.getCustomParam("fileSize");
        if (StringUtils.isBlank(fileSize) || NumberUtils.toDouble(fileSize) <= 0) {
            throw new ColumnCheckException(ColumnExceptionResponse.FILE_SIZE);
        }
        String fileTypes = super.getCustomParam("fileTypes");
        if (StringUtils.isBlank(fileTypes)) {
            throw new ColumnCheckException(ColumnExceptionResponse.FILE_TYPE);
        }
        cubeColumnDto.setMinLength(null);
        cubeColumnDto.setMinLength(null);
        cubeColumnDto.setMaxValue(null);
        cubeColumnDto.setMinValue(null);
        cubeColumnDto.setDecimalNum(null);

        ColumnExtTool.initCubeColumnExt(cubeColumnDto, "文件大小", "fileSize", fileSize);
        ColumnExtTool.initCubeColumnExt(cubeColumnDto, "多文件", "manyYn", BooleanUtils.isTrue(super.getCustomParam("manyYn")) ? "Y" : "N");
        ColumnExtTool.initCubeColumnExt(cubeColumnDto, "文件类型", "fileTypes", fileTypes.toLowerCase());
        this.checkExt(fileTypes);
    }


    protected abstract void checkExt(String fileTypes);
}
